package com.example.myfirstapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import com.example.myfirstapplication.adapter.MyViewPagerAdapter
import com.example.myfirstapplication.fragment.HomeFragment
import com.example.myfirstapplication.fragment.VideoFragment
import com.example.myfirstapplication.fragment.MineFragment
import com.example.myfirstapplication.utils.BaseActivity
import com.example.myfirstapplication.utils.Tag
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.activity_view_pager_tab_layout.*

class ViewPagerTabLayout : BaseActivity(), TabLayout.OnTabSelectedListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager_tab_layout)
        val tabTag= arrayOf("首页","地图","我的")
        var fmlist = listOf<Fragment>(HomeFragment(), VideoFragment(),MineFragment())
        viewPager.adapter = MyViewPagerAdapter(fmlist, supportFragmentManager)
        tabLayout.setupWithViewPager(viewPager)
        for (i in 0..2)
        {
            Log.i(Tag,i.toString())
            Log.i(Tag,tabTag[i])
            tabLayout.getTabAt(i)?.text = tabTag[i]
        }
        viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))
    }
    override fun onTabReselected(p0: TabLayout.Tab?) {
        TODO("Not yet implemented") //Tab重新选择时候回调
    }
    override fun onTabUnselected(p0: TabLayout.Tab?) {
        TODO("Not yet implemented") //Tab未被选择时候回调
    }
    override fun onTabSelected(p0: TabLayout.Tab?) {
        //Tab被选的时候回调
        viewPager.currentItem = p0!!.position
    }
}

